package com.weiwu.weiwustore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiwu.weiwustore.entity.Notice;
import com.weiwu.weiwustore.entity.Role;
import com.weiwu.weiwustore.entity.Store;
import com.weiwu.weiwustore.entity.User;
import com.weiwu.weiwustore.mapper.NoticeMapper;
import com.weiwu.weiwustore.mapper.StoreMapper;
import com.weiwu.weiwustore.mapper.UserMapper;
import com.weiwu.weiwustore.pojo.NoticeListVo;
import com.weiwu.weiwustore.service.NoticeService;
import com.weiwu.weiwustore.utils.ObjectFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.weiwu.weiwustore.utils.ObjectFormatUtil.toInteger;


@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    StoreMapper storeMapper;

    @Override
    public Integer insertNotice(Map<String, Object> map) {
        return noticeMapper.insert(new Notice(String.valueOf(map.get("title")), String.valueOf(map.get("content")), toInteger(map.get("storeid"))));
    }

    @Override
    public Integer deleteNoticeById(Integer id) {
        return noticeMapper.deleteById(id);
    }

    @Override
    public Integer update(Map<String, Object> map) {
        if (!CheckParams(map))
            return -1;
        QueryWrapper<Store> wrapper = new QueryWrapper<>();
        Integer storeid = 0;
        if (String.valueOf(map.get("storeid")).contains("仓")) {
            if (String.valueOf(map.get("storeid")).equals("全部仓库")) {
                storeid = -1;
            } else {
                wrapper.eq("sname", String.valueOf(map.get("storeid")));
                Store role = storeMapper.selectOne(wrapper);
                storeid = role.getId();
            }
        } else {
            storeid = toInteger(map.get("storeid"));
        }
        Notice notice = new Notice(toInteger(map.get("id")), String.valueOf(map.get("title")), String.valueOf(map.get("content")), storeid);
        return noticeMapper.updateById(notice);
    }

    @Override
    public Integer findCount() {
        return noticeMapper.selectCount(null);
    }

    @Override
    public List<NoticeListVo> findNoticeByCondition(Map<String, Object> map) {
        Integer storeid = ObjectFormatUtil.toInteger(map.get("storeid"));
        List<Notice> lists = noticeMapper.selectList(
                new QueryWrapper<Notice>().and(i -> i.eq("storeid", storeid).or().eq("storeid", -1)));
        return singlePack(lists);
    }

    @Override
    public List<NoticeListVo> getnoticelist(Map<String, Object> map) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        String select;
        Integer size = 10, page = 1;
        IPage pageP = new Page(page, size);
        if (map.containsKey("size")) {
            size = toInteger(map.get("size"));
        }
        if (map.containsKey("page")) {
            page = ObjectFormatUtil.toInteger(map.get("page"));
        }
        pageP.setSize(size);
        pageP.setCurrent(page);
        if (map.containsKey("select")) {
            select = (String) map.get("select");
            wrapper.and(i -> i.like("title", select).or().like("content", select));
        }
        IPage list = noticeMapper.selectPage(pageP, wrapper);
        return singlePack02(list.getRecords());
    }

    private List<NoticeListVo> singlePack(List<Notice> lists) {
        List<NoticeListVo> res = new ArrayList<>();
        for (Notice n : lists) {
            res.add(new NoticeListVo(n.getId(), n.getTitle(), n.getContent(), n.getUpdatetime()));
        }
        return res;
    }

    private List<NoticeListVo> singlePack02(List<Notice> lists) {
        List<NoticeListVo> res = new ArrayList<>();
        String storeid = "";
        for (Notice n : lists) {
            if (n.getStoreid() == -1) {
                storeid = "全部仓库";
            } else {
                storeid = storeMapper.selectById(n.getStoreid()).getSname();
            }
            res.add(new NoticeListVo(n.getId(), n.getTitle(), n.getContent(),
                    n.getCreatetime(), n.getUpdatetime(), storeid));
        }
        return res;
    }

    private boolean CheckParams(Map<String, Object> map) {
        if (!map.containsKey("id")) {
            return false;
        }
        if (!map.containsKey("title")) {
            return false;
        }
        if (!map.containsKey("content")) {
            return false;
        }
        if (!map.containsKey("storeid")) {
            return false;
        }
        return true;
    }
}
